package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.common.exception.CustException;
import com.heima.model.common.dto.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dto.LoginDTO;
import com.heima.model.user.pojo.ApUser;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.service.ApUserLoginService;
import com.heima.utils.common.AppJwtUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.Map;

@Service
public class ApUserLoginServiceImpl implements ApUserLoginService {
    @Autowired
    ApUserMapper apUserMapper;

    /**
     * app端登录
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult login(LoginDTO dto) {

        /**
         * app端登录
         * @param dto
         * @return
         */
        //1.校验参数
        if (StringUtils.isNotBlank(dto.getPhone()) && StringUtils.isNotBlank(dto.getPassword())) {
            ApUser apUser = apUserMapper.selectOne(Wrappers.<ApUser>lambdaQuery()
                    .eq(ApUser::getPhone, dto.getPhone()));
            if (apUser == null) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "请检查手机号");
            }
            // 数据库密码
            String dbpassword = apUser.getPassword();
            String newPassword = DigestUtils.md5DigestAsHex((dto.getPassword() + apUser.getSalt()).getBytes());
            if (!dbpassword.equals(newPassword)) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "手机号或密码错误");
            }
            Map<String, Object> map = new HashMap<>();
            apUser.setPassword("");
            apUser.setSalt("");
            map.put("token", AppJwtUtil.getToken(apUser.getId().longValue()));
            map.put("user", apUser);
            return ResponseResult.okResult(map);
        } else {
            if (dto.getEquipmentId() == null) {
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
            }
            Map<String, Object> map = new HashMap<>();
            // 通过设备ID登录的 userId存0
            map.put("token", AppJwtUtil.getToken(0L));
            return ResponseResult.okResult(map);
        }
    }
}

/*
    // 1 如果手机号和密码不为空  用账户密码登录
    String phone = dto.getPhone();
    String password = dto.getPassword();
        if (StringUtils.isNotBlank(phone) && StringUtils.isNotBlank(password)) {
                // 根据手机号查询用户
                ApUser apUser = apUserMapper.selectOne(Wrappers.<ApUser>lambdaQuery().eq(ApUser::getPhone, phone));
        if (apUser == null) {
        CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST, "用户不存在");
        }
        // 判断输入的密码和用户名是否一致
        String md5Word = DigestUtils.md5DigestAsHex((password + apUser.getSalt()).getBytes());
        if (!md5Word.equals(apUser.getPassword())) {
        CustException.cust(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
        }
        // 检查用户状态  1 true  0 false
        //颁发token
        String token = AppJwtUtil.getToken(Long.valueOf(apUser.getId()));
        // 返回结果  data{token:"",user:""}
        Map map = new HashMap<>();
        map.put("token", token);
        apUser.setPassword("");
        apUser.setSalt("");
        map.put("user", apUser);
        return ResponseResult.okResult(map);
        } else {
        // 2 如果手机号和密码为空  使用设备id登录
        // 检查设备id
        if (dto.getEquipmentId() == null) {
        CustException.cust(AppHttpCodeEnum.PARAM_INVALID, "用户名密码和设备id 全部为空");
        }
        // 直接颁发token  用户id  存为0
        String token = AppJwtUtil.getToken(0L);
        //返回结果  data{token:""}
        Map map = new HashMap<>();
        map.put("token", token);

        return ResponseResult.okResult(map);
        }*/
